package stack.be.touch.adapter;

import java.util.ArrayList;

import stack.be.touch.dto.SiteDTO;
import stack.be.touch.R;
import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SitesAdapter extends ArrayAdapter<SiteDTO>{
	Activity ctxt;
	ArrayList<SiteDTO> sites;
	int imgID;
	
	
	public SitesAdapter(Activity ctxt, int resource,
			ArrayList<SiteDTO> sites) {
		super(ctxt, resource);
		
		this.ctxt = ctxt;
		this.sites = sites;
	}
	
	private static class ViewHolder {
		protected TextView siteName;
		protected TextView siteEndpoint;
		protected ImageView siteImg;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return sites.size();
	}

	@Override
	public SiteDTO getItem(int position) {
		return sites.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder = new ViewHolder();
		
		if(row == null){
			LayoutInflater inflater = ctxt.getLayoutInflater();
			row = inflater.inflate(R.layout.row, null);
			holder.siteName = (TextView)row.findViewById(R.id.Name);
			holder.siteEndpoint = (TextView)row.findViewById(R.id.Endpoint);
			row.setTag(holder);
		}else{
			holder = (ViewHolder)row.getTag();
		}
		
		holder.siteName.setText(sites.get(position).getName());
		holder.siteEndpoint.setText(sites.get(position).getEndpoint());
		
//		ImageView icon = (ImageView)row.findViewById(R.id.icon);
//		imgID = ctxt.getResources().getIdentifier(items[3*position], "drawable", ctxt.getPackageName());
//		icon.setImageResource(imgID);
		
		return row;
	}
	
	
}// .FunnyLookingAdapter


